package com.mera.tektronix.csviewer.services

import org.apache.commons.logging.LogFactory
import org.springframework.context.MessageSource

import com.mera.tektronix.csviewer.common.api.*
import com.mera.tektronix.csviewer.common.dto.*
import com.mera.tektronix.csviewer.common.exception.*
import com.mera.tektronix.csviewer.domain.*

class EmployeeService {
	static transactional = true
	private static final log = LogFactory.getLog(EmployeeService.class)
	
	MessageSource messageSource
	IEmployeeService employeeServiceProxy
	IProjectService projectServiceProxy
	
	def getList(params) {
		log.debug(params)
		
		return employeeServiceProxy.getListByProjectId(Long.valueOf(params.id)).collect{[
			id   : ("_EMP_" + it.id),
			text : it.name,
			leaf: true
		]}
	}
	
	def getElement(params) {
		log.debug(params)
		
		EmployeeDto emp = null;
		
		try {
			emp = employeeServiceProxy.getEmployeeById(Long.valueOf(params.id));
		} catch(Exception ex) {
			return [
				success      : 'false',
				errorMessage : ex.getLocalizedMessage()
			];
		}
		
		if(emp == null) {
			return [
				success      : 'false',
				errorMessage : "No Employee has been found."
			]
		}
		
		return [
			id           : emp.id,
			name         : emp.name,
			birthDate    : emp.birthDate,
			phone        : emp.phone,
			email        : emp.email,
			hiredDate    : emp.hiredDate,
			position     : emp.position,
			project      : emp.projectName,
			projectId    : emp.projectId,
			department   : emp.departmentName,
			departmentId : emp.departmentId,
			photo        : emp.photo,
			success      : 'true'
		];
	}
	
	def addElement(params) {
		log.debug(params)
		
		ProjectDto project = null;
		
		try {
			project = projectServiceProxy.getProjectById(Long.valueOf(params.project));
		} catch(Exception ex) {
			return [
				success      : 'false',
				errorMessage : ex.getLocalizedMessage()
			];
		}
		
		if(project == null) {
			return [
				success      : 'false',
				errorMessage : "No Project has been found."
			]
		} else {
			EmployeeDto emp = new EmployeeDto();
			
			if(params.id != null) emp.id = Long.valueOf(params.id);
			emp.name = params.name;
			emp.birthDate = params.birthDate;
			emp.phone = params.phone;
			emp.email = params.email;
			emp.hiredDate = params.hiredDate;
			emp.position = params.position;
			emp.projectId = project.id;
			
			try {
				employeeServiceProxy.addEmployee(emp);
			} catch(LogicException lex) {
				def errorList = lex.getErrors();
				
				return [
					success      : 'false',
					errors       : errorList
				]
			}
		}
		
		return [
			success : 'true'
		]
	}
	
	def updateElement(params) {
		return addElement(params);
	}
	
	def deleteElement(params) {
		log.debug(params)

		try {
			employeeServiceProxy.deleteEmployee(Long.valueOf(params.id));
		} catch(Exception ex) {
			return [
				success      : 'false',
				errorMessage : ex.getLocalizedMessage()
			];
		}

		return [success : 'true'];
	}
}
